﻿Imports MySql.Data.MySqlClient

Public Class DatabaseFuncs
    Dim loginservice As New LoginService
    Dim ConnectieStr = loginservice.getConnectionString
    Dim QueryStr As String = ""

    Function setup()
        QueryStr = "SELECT * FROM `users`"

        'SQL lezen
        Dim SQLConn As New MySqlConnection 'De SQL verbinding
        Dim SQLCmd As New MySqlCommand 'Het commando       

        Try
            SQLConn.ConnectionString = ConnectieStr
            SQLConn.Open() 'Verbinding openen

            With SQLCmd
                .CommandText = QueryStr
                .CommandType = CommandType.Text
                .Connection = SQLConn
                .ExecuteNonQuery()
            End With

        Catch ex As Exception
            MsgBox("De verbinding is niet gelukt: " + ex.Message)
            Return "False"
        Finally
            SQLConn.Close() 'Verbinding sluiten
        End Try

        Return "True"
    End Function


    Function query(ByVal QueryStr As String, ByVal DatabaseNr As Integer) 'DatabaseNr 1 = klanten.vb, DatabaseNr 2 = users.vb DatabaseNr 3 = incidents.vb
        'SQL lezen
        Dim SQLConn As New MySqlConnection 'De SQL verbinding
        Dim SQLCmd As New MySqlCommand 'Het commando
        Dim Adapter As MySqlDataAdapter = Nothing
        Try
            SQLConn.ConnectionString = ConnectieStr
            Adapter = New MySqlDataAdapter(QueryStr, SQLConn) 'Adapter, om een dataset te vullen met data van de database.

            Dim TempDataSet As Object
            Select Case DatabaseNr
                Case 1
                    TempDataSet = Customers.KlantenDataSet 'Alle data wordt gelijk in de dataset van de klanten gezet.
                Case 2
                    TempDataSet = Users.UsersDataSet
                Case 3
                    TempDataSet = Incidents.IncidentsDataSet
            End Select

            TempDataSet.Clear()
            SQLConn.Open()
            Adapter.Fill(TempDataSet, "Table")

        Catch ex As Exception
            MsgBox("De query is mislukt: " + ex.Message)
            Return "False"
        Finally
            SQLConn.Close() 'Verbinding sluiten
            SQLConn.Dispose()
        End Try
        Return "True"
    End Function
End Class
